﻿/************************************************************************

 This file is part of EscapeFromPrisonPlanet.

 EscapeFromPrisonPlanet is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 EscapeFromPrisonPlanet is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2012 Scott Bevin. all rights reserved

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LudumDare25Project.GameScreens
{
    internal class MainMenuScreen : GameScreen
    {
        private Texture2D logoTexture;
        private Menu menu;

        private GameScreen gamescreenToTrigger = null;
        private WhiteFadeScreen whiteFade = null;

        public MainMenuScreen()
            : base(true)
        {
        }

        public override void OnAdded()
        {
            logoTexture = ContentManager.Load<Texture2D>("Img/GameLogo");
            menu = new Menu(ContentManager.Load<SpriteFont>("Fonts/Linds24"), ContentManager);

            menu.AddEntry("New Game", this.NewGame);
            menu.AddEntry("Options", this.Options);
            menu.AddEntry("How To Play / Credits", this.HelpMenu);
            menu.AddEntry("Quit Game", this.QuitGame);
            menu.AddNonClickEntry(" ", null);
            menu.AddNonClickEntry("www.gameXcore.co.uk", null);
            menu.AddNonClickEntry("scott@gameXcore.co.uk", null);

            base.OnAdded();
        }

        public override void Update(float dt, InputManager input)
        {
            menu.Update(dt, input, this.Game.GraphicsDevice);

            /*if(whiteFade == null)
            {
                if (input.MouseButtonStates[(int)InputManager.MouseButtons.LeftButton].Released ||
                    input.MouseButtonStates[(int)InputManager.MouseButtons.RightButton].Released)
                {
                    whiteFade = new WhiteFadeScreen();
                    gamescreenToTrigger = new GameplayScreen();

                    //gamescreenToTrigger = new HelpScreen();

                    GameScreenManager.AddGameScreen(whiteFade, null, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop);
                }
            }*/

            if (whiteFade != null && whiteFade.State == WhiteFadeScreen.States.FadedIn)
            {
                whiteFade.FadeOut();
                GameScreenManager.AddGameScreen(gamescreenToTrigger, whiteFade, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.BelowRef);
                this.KillScreen = true;
            }

            base.Update(dt, input);
        }

        public override void Render(float dt, GraphicsDevice gd, SpriteBatch sprites)
        {
            Vector2 basePosition = new Vector2(((float)gd.PresentationParameters.BackBufferWidth) * 0.5f,
                ((float)gd.PresentationParameters.BackBufferHeight) * 0.25f);

            sprites.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);

            sprites.Draw(logoTexture, basePosition, null, Color.White, 0.0f,
                new Vector2(((float)logoTexture.Width) * 0.5f, ((float)logoTexture.Height) * 0.5f), Vector2.One, SpriteEffects.None, 0.0f);

            sprites.End();

            menu.Render(gd, sprites);

            base.Render(dt, gd, sprites);
        }

        private void NewGame(Menu.MenuEntry entry)
        {
            whiteFade = new WhiteFadeScreen();
            gamescreenToTrigger = new GameplayScreen();

            //gamescreenToTrigger = new HelpScreen();

            GameScreenManager.AddGameScreen(whiteFade, null, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop);
        }

        private void Options(Menu.MenuEntry entry)
        {
            whiteFade = new WhiteFadeScreen();
            gamescreenToTrigger = new OptionsMenuScreen();

            //gamescreenToTrigger = new HelpScreen();

            GameScreenManager.AddGameScreen(whiteFade, null, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop);
        }

        private void HelpMenu(Menu.MenuEntry entry)
        {
            whiteFade = new WhiteFadeScreen();
            gamescreenToTrigger = new HelpScreen();

            GameScreenManager.AddGameScreen(whiteFade, null, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop);
        }

        private void QuitGame(Menu.MenuEntry entry)
        {
            this.Game.Exit();
        }
    }
}